/**
 * @FileName: WhieCardController.java
 * @Author
 * @Description:
 * @Date 2016年1月4日 下午2:11:46
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.admin;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Blankcard;
import cn.hpclub.server.service.BlankcardExportService;
import cn.hpclub.server.service.BlankcardService;
import cn.hpclub.server.util.InterfaceUtil;

public class BlankcardreportController extends BaseAdminController<Blankcard>{
    private static final String SQL_ORDER_BY               = "ORDER BY org.id ASC";
    private static final String SQL_SELECT_COLUMNS_4_QUERY = "org.id as orgId,(select gan.DisplayName from organization as gan where gan.id=org.parentid) as parentname,(select class_name from organization_class where id=org.WebsiteId) as class_name,NumCode, DisplayName, SIMCardCount";
    private static Logger       log                        = LoggerFactory.getLogger(BlankcardreportController.class);

    /**
     * @Title: 获取白卡列表
     */
    public void list(){
        log.info("BlankcardReportController list");
        String keyword = getFilterKeyword();
        if(isExportCmd()){
            export(keyword);
        } else{
            showList(keyword);
        }
    }

    private void showList(String keyword){
        Page<Blankcard> pager = BlankcardService
                .findBlankcardByPage(getParaToInt(Constant.Ui.PAGER_NUMBER, 1),
                                     getParaToInt(Constant.Ui.PAGER_SIZE, 10), InterfaceUtil.getOrgId(),
                                     getSqlExByKeyword(keyword), SQL_SELECT_COLUMNS_4_QUERY, SQL_ORDER_BY);
        setAttr("pager", pager);
        render("/admin/blankcard_report_list.tpl");
    }

    /**
     * @Title: getSqlExByKeyword
     * @Description:根据关键字获取查询扩展条件
     * @param keyword
     * @return String
     */
    private String getSqlExByKeyword(String keyword){
        String sqlEx = "";
        if(!StrKit.isBlank(keyword)){
            sqlEx = " and org.DisplayName like '%" + keyword + "%'";
        }
        return sqlEx;
    }

    private boolean isExportCmd(){
        return this.getParaToInt("isExport", 0) == 1;
    }

    /**
     * @return
     */
    private String getFilterKeyword(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        String keyword = "";
        for(Entry<String, String> item : searchInfo.entrySet()){
            if(item.getKey().equals("site_name")){
                keyword = item.getValue();
            }
        }
        setAttrs(searchInfo);
        return keyword;
    }

    private void export(String keyword){
        JSONObject jo = BlankcardExportService.exportData(keyword);
        renderJson(jo.toJSONString());
    }
}
